package cn.iocoder.yudao.module.infra.controller.admin.face;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.controller.admin.face.vo.FaceConfigCreateReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.face.vo.FaceConfigPageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.face.vo.FaceConfigRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.face.vo.FaceConfigUpdateReqVO;
import cn.iocoder.yudao.module.infra.convert.face.FaceConfigConvert;
import cn.iocoder.yudao.module.infra.dal.dataobject.face.FaceConfigDO;
import cn.iocoder.yudao.module.infra.service.face.FaceConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


/**
 * 管理后台 - 人脸识别配置
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 人脸识别配置")
@RestController
@RequestMapping("/infra/face-config")
@Validated
@Slf4j
public class FaceConfigController {

    @Resource
    private FaceConfigService faceConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建人脸识别配置")
    @PreAuthorize("@ss.hasPermission('infra:face-config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody FaceConfigCreateReqVO createReqVO) {
        FaceConfigDO faceConfig = FaceConfigConvert.INSTANCE.convert(createReqVO);
        Long id = faceConfigService.createConfig(faceConfig);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新人脸识别配置")
    @PreAuthorize("@ss.hasPermission('infra:face-config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody FaceConfigUpdateReqVO updateReqVO) {
        FaceConfigDO faceConfig = FaceConfigConvert.INSTANCE.convert(updateReqVO);
        faceConfigService.updateConfig(faceConfig);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除人脸识别配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:face-config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") @NotNull Long id) {
        faceConfigService.deleteConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得人脸识别配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:face-config:query')")
    public CommonResult<FaceConfigRespVO> getConfig(@RequestParam("id") @NotNull Long id) {
        FaceConfigDO faceConfig = faceConfigService.getConfig(id);
        return success(FaceConfigConvert.INSTANCE.convert(faceConfig));
    }

    @GetMapping("/get-enabled")
    @Operation(summary = "获得启用的人脸识别配置")
    @PreAuthorize("@ss.hasPermission('infra:face-config:query')")
    public CommonResult<FaceConfigRespVO> getEnabledFaceConfig() {
        FaceConfigDO config = faceConfigService.getEnabledConfig();
        return success(BeanUtils.toBean(config, FaceConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得人脸识别配置分页")
    @PreAuthorize("@ss.hasPermission('infra:face-config:query')")
    public CommonResult<PageResult<FaceConfigRespVO>> getFaceConfigPage(@Valid FaceConfigPageReqVO pageReqVO) {
        PageResult<FaceConfigDO> pageResult = faceConfigService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FaceConfigRespVO.class));
    }

}